"""
帮助文本加载器
用于从JSON配置文件加载帮助和关于信息
"""

import json
import os
from settings import BASE_DIR


class HelpLoader:
    """帮助文本加载器类"""
    
    def __init__(self):
        """初始化帮助文本加载器"""
        self.help_file = os.path.join(BASE_DIR, 'config', 'help_text.json')
        self._help_data = None
        self._load_help_data()
    
    def _load_help_data(self):
        """加载帮助文本数据"""
        try:
            with open(self.help_file, 'r', encoding='utf-8') as f:
                self._help_data = json.load(f)
        except FileNotFoundError:
            print(f"警告：帮助文本文件未找到: {self.help_file}")
            self._help_data = {}
        except json.JSONDecodeError as e:
            print(f"警告：帮助文本文件格式错误: {e}")
            self._help_data = {}
        except Exception as e:
            print(f"警告：加载帮助文本失败: {e}")
            self._help_data = {}
    
    def get_help_text(self):
        """获取帮助文本"""
        if not self._help_data or 'help' not in self._help_data:
            return "帮助信息加载失败"
        
        help_data = self._help_data['help']
        help_text = f"{help_data['title']}\n\n"
        
        # 遍历所有章节
        for section_key, section_data in help_data['sections'].items():
            help_text += f"【{section_data['title']}】\n"
            
            if 'items' in section_data:
                for item in section_data['items']:
                    help_text += f"• {item}\n"
            elif 'steps' in section_data:
                for i, step in enumerate(section_data['steps'], 1):
                    help_text += f"{i}. {step}\n"
            
            help_text += "\n"
        
        return help_text.strip()
    
    def get_about_text(self):
        """获取关于文本"""
        if not self._help_data or 'about' not in self._help_data:
            return "关于信息加载失败"
        
        about_data = self._help_data['about']
        about_text = f"{about_data['title']}\n        \n"
        about_text += f"版本: {about_data['version']}\n"
        about_text += f"更新日期: {about_data['update_date']}\n\n"
        
        # 遍历所有章节
        for section_key, section_data in about_data['sections'].items():
            about_text += f"{section_data['title']}:\n"
            
            for item in section_data['items']:
                about_text += f"• {item}\n"
            
            about_text += "\n"
        
        # 添加支持信息
        if 'support' in about_data:
            support = about_data['support']
            about_text += f"技术支持: {support['team']}\n"
            about_text += f"项目地址: {support['project_url']}\n"
        
        return about_text.strip()
    
    def get_shortcuts_text(self):
        """获取详细快捷键文本"""
        if not self._help_data or 'help' not in self._help_data:
            return "快捷键信息加载失败"
        
        help_data = self._help_data['help']
        if 'shortcuts_detailed' not in help_data:
            return "快捷键信息未找到"
        
        shortcuts_data = help_data['shortcuts_detailed']
        shortcuts_text = f"{shortcuts_data['title']}\n\n"
        
        # 遍历所有章节
        for section_key, section_data in shortcuts_data['sections'].items():
            shortcuts_text += f"【{section_data['title']}】\n"
            
            for item in section_data['items']:
                shortcuts_text += f"{item}\n"
            
            shortcuts_text += "\n"
        
        return shortcuts_text.strip()
    
    def reload(self):
        """重新加载帮助文本数据"""
        self._load_help_data()


# 创建全局实例
help_loader = HelpLoader()
